package com.dek.grpc.header;

import io.grpc.*;

import java.util.logging.Logger;

public class HeaderServerInterceptor implements ServerInterceptor {


    private static final Logger logger = Logger.getLogger(HeaderServerInterceptor.class.getName());

    static final Metadata.Key<String> CUSTOM_HEADER_KEY =
            Metadata.Key.of("custom_server_header", Metadata.ASCII_STRING_MARSHALLER);

    @Override
    public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> serverCall, Metadata metadata,
                                                                 ServerCallHandler<ReqT, RespT> serverCallHandler) {
        logger.info("header received from client:" + metadata);
        return serverCallHandler.startCall(new ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT> (serverCall) {
            @Override
            public void sendHeaders(Metadata headers) {
                headers.put(CUSTOM_HEADER_KEY, "dek");
                super.sendHeaders(headers);
            }
        }, metadata);
    }
}
